class main_scene extends eui.Component implements eui.UIComponent {

	public itemPlayer: player_item;
	public itemEnemy: enemy_item;

	private isAttacking=false;

	listTreasure:eui.List;
	dataGroupTreasure:eui.DataGroup;
	public dataGroupTreasureCollection = new eui.ArrayCollection();

	imgMoneyBg:eui.Image;
	lbMoney:eui.Label;

	imgMoneyBgWidth:number;


	public constructor() {
		super();
	}

	protected partAdded(partName: string, instance: any): void {
		super.partAdded(partName, instance);
	}


	protected childrenCreated(): void {
		super.childrenCreated();
		this.imgMoneyBgWidth=this.imgMoneyBg.width;
		this.dataGroupTreasure.visible=false;
		this.listTreasure.visible=false;
		Pomelo.ins().on(RequestRoute.FightOnAttack, async (e) => {
			if(this.isAttacking){
				await TimerUtils.delay(500);
			}
			this.isAttacking=true;
			if (e.activeId === String(GameGlobal.ins().user.player.id)) {
				await this.itemPlayer.runAttack(this.itemEnemy.imgAvatar, e.hurt.hurt);
			    this.itemEnemy.getHurt(e.hurt.hurt);
			}
			else if (e.activeId === String(GameGlobal.ins().enemy.id)) {
				await this.itemEnemy.runAttack(this.itemPlayer.imgAvatar, e.hurt.hurt);
				 this.itemPlayer.getHurt(e.hurt.hurt);
			}
			this.isAttacking=false;
		})
		Pomelo.ins().on(RequestRoute.FightOnFightOver, async (e) => {
			await TimerUtils.delay(2500);
			GameGlobal.ins().user.player=e;
			await TimerUtils.delay(500);
			this.itemPlayer.fightEnd(e.isDied);
			await TimerUtils.delay(1000);
			this.matchPlayer();
		})

		ClientManager.ins().login().then(async () => {
			await ClientManager.ins().getConfig();
			await ClientManager.ins().getTreasure();
			let dataArr:any[] =[];
			for(let k in GameGlobal.ins().treasureConfig){
				dataArr.push({imgUrl:GameGlobal.ins().treasureConfig[k].imgBuyUrl,price:""+GameGlobal.ins().treasureConfig[k].price
			,qualityType:k});
			}
 				await this.itemPlayer.refresh();
			 	 this.refreshMoney();
				  this.refreshTreasure();

		let euiArr:eui.ArrayCollection = new eui.ArrayCollection(dataArr)
			this.listTreasure.itemRenderer = treasure_buy_item;

			this.listTreasure.dataProvider=euiArr;
			this.listTreasure.visible=true;
			this.dataGroupTreasure.itemRenderer=treasure_item;
			this.dataGroupTreasure.dataProvider=this.dataGroupTreasureCollection;
		 	this.dataGroupTreasure.visible=true;

			  this.matchPlayer();
			 
		})
  
		Events.register(Events.Type.TAP_TREASURE,async(e)=>{
			 	let success= await ClientManager.ins().buyTreasure(e.data);
				 if(success){
					 this.refreshMoney();
					 this.createTreasure(success);
				 }
		},this);
	 

	}
	async matchPlayer() {
		let data: any = await Pomelo.ins().request(RequestRoute.FightMatchPlayer);
		GameGlobal.ins().enemy = data.data;
		await this.itemEnemy.refresh();
		await TimerUtils.delay(1500);
		Pomelo.ins().request(RequestRoute.FightStart);
	}

	refreshMoney(){
	 		 this.imgMoneyBg.width=String(GameGlobal.ins().user.player.money).length*15+this.imgMoneyBgWidth;
			  this.lbMoney.text=String(GameGlobal.ins().user.player.money);
	}
	 refreshTreasure(){
		this.dataGroupTreasureCollection.removeAll();
		if(GameGlobal.ins().treasure){
			GameGlobal.ins().treasure.map((t)=>{
				this.createTreasure(t);
			});
		}
	}

	createTreasure(treasureData){
		console.log(treasureData);
		// console.log("w..h",this.dataGroupTreasure.width,this.dataGroupTreasure.height);
		let posX=Math.random()*this.dataGroupTreasure.width;
	 	let posY=Math.random()*this.dataGroupTreasure.height;
		 let item={
			  imgUrl:GameGlobal.ins().treasureConfig[treasureData.qualityType].imgUrl,
			  qualityType:treasureData.qualityType,
			  x:posX,
			  y:posY,
			  id:treasureData.id
		  	};
		 let index=-1;
		 for(let i=0;i<this.dataGroupTreasureCollection.source.length;i++){
		 	 if(posY<this.dataGroupTreasureCollection.source[i].y){
				  index=i;
				  break;
			}
		 }
		//  this.dataGroupTreasureCollection.addItem( item);
		 if(index===-1){
	    	this.dataGroupTreasureCollection.addItem(item);
		 }
		 else{
			 this.dataGroupTreasureCollection.addItemAt(item,index);
		 	 console.log("index..",index,this.dataGroupTreasureCollection.source)
		 }
		 
	   
	}


}